package com.system.healthproject.model.admin.service.impl;

import com.system.healthproject.bean.*;
import com.system.healthproject.mapper.ConsultingMapper;
import com.system.healthproject.mapper.DoctorMapper;
import com.system.healthproject.mapper.LoginMapper;
import com.system.healthproject.mapper.PositionMapper;
import com.system.healthproject.model.admin.service.DoctorService;
import com.system.healthproject.pojo.Position;
import com.system.healthproject.pojo.Transaction;
import com.system.healthproject.service.RoleService;
import com.system.healthproject.utils.Md5Utils;
import com.system.healthproject.utils.SimpleFormatutiles;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    LoginMapper loginMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ConsultingMapper consultingMapper;
    /**
     * 添加医生
     * @param parameterDoctor
     * @return
     */
    @Override
    @Transactional
    public String addDoctor(ParameterDoctor parameterDoctor) {
        doctorMapper.addDoctor(parameterDoctor);
        Integer doctorID = parameterDoctor.getId();
        parameterDoctor.setPassword(Md5Utils.md5("123456"));
        loginMapper.addLogin(parameterDoctor);
        Integer  loginID = parameterDoctor.getId();
        boolean b = roleService.addUserRole(parameterDoctor.getId(),parameterDoctor.getDoctorRole());
        if(b){
            return "成功";
        }return "失败";
    }


    /**
     * 修改医生信息
     * @param paramterUpdateDoctorInfo
     * @return
     */
    @Override
    public String updateDoctor(ParamterUpdateDoctorInfo paramterUpdateDoctorInfo) {
        int row = doctorMapper.updateDoctor(paramterUpdateDoctorInfo);
        System.out.println(paramterUpdateDoctorInfo.getDoctorRole());
        Integer doctorRole = paramterUpdateDoctorInfo.getDoctorRole();
        if(doctorRole!=null){
            //通过医生ID查询登录ID
            Integer loginId = loginMapper.selectLoginId(paramterUpdateDoctorInfo.getDoctorId());
            System.out.println(loginId);
            boolean b =  roleService.userChangeRole(loginId, doctorRole);
            System.out.println(b);
            if(b){
                return "成功";
            }return "失败";
        }
        return "成功";

    }

    /**
     * 添加诊疗活动
     * @param paramAddConsulting
     * @return
     */
    @Transactional
    @Override
    public String addConsulting(ParamAddConsulting paramAddConsulting) {
        consultingMapper.addConsulting(paramAddConsulting);
        int row = consultingMapper.addConsultingRemindPatient( paramAddConsulting.getPatientId(),paramAddConsulting.getId());
        return row>0?"成功":"失败";
    }

    /**
     * 查询所有医生交易信息
     * @param currentPage
     * @param limit
     * @return
     */
    @Override
    public List<ResultDoctorTransaction> selectDoctorTransaction(Integer currentPage, Integer limit) {
        List<ResultDoctorTransaction> resultDoctorTransactions = doctorMapper.selectDoctorTransaction((currentPage - 1) * limit, limit);
        return resultDoctorTransactions;
    }

    /**
     * 医生消除核桃币
     * @param doctorId  医生ID
     * @param walnutCoinNumber  要消除的核桃币数量
     * @param remarks   备注
     * @return
     */
    @Override
    @Transactional
    public String eliminate(Integer doctorId, Double walnutCoinNumber, String remarks) {
        //根据医生ID查询loginId
        Integer loginId = loginMapper.selectLoginId(doctorId);
        //判断医生核桃币余额 >= 要消除的核桃币数量
        if(doctorMapper.selectSurplus(loginId)>=walnutCoinNumber){
            doctorMapper.eliminate(loginId,walnutCoinNumber);
            Transaction transaction =new Transaction();
            transaction.setLoginId(loginId);
            transaction.setTransactionType("平台消除");
            transaction.setTransactionVirtualmoney(walnutCoinNumber);
            transaction.setTransactionTime(new Date());
            transaction.setTransactionState(1);
            transaction.setCollectionLoginid(0);
            transaction.setTransactionStatuetype(3);
            int row = doctorMapper.addDoctorTransaction(transaction);
            return row>0?"成功":"失败";
        }return "核桃币不足";


    }

    /**
     * 查询医生核桃币数量
     * @param doctorId
     * @return
     */
    @Override
    public Double selectSurplus(Integer doctorId) {
        //根据医生ID查询loginId
        Integer loginId = loginMapper.selectLoginId(doctorId);
        return  doctorMapper.selectSurplus(loginId);
    }

    /**
     * 所有医生交易记录数据条数
     * @return
     */
    @Override
    public Integer countDoctorTransaction() {
        return  doctorMapper.countDoctorTransaction();
    }

    /**
     * 查询医生列表
     * @param doctorName  姓名
     * @param hospitalId  医院ID
     * @param doctorStart  账号状态
     * @param startTime  开始时间
     * @param endTime   结束时间
     * @param doctorSex  性别
     * @param currentPage  当前页
     * @param limit  每页显示条数
     * @return
     */
    @Override
    public List<ResultDoctor> selectDoctor(String doctorName, Integer hospitalId, Integer doctorStart, String startTime,
                                String endTime, Integer doctorSex, Integer currentPage, Integer limit) {
        ParamSelectDoctor  paramSelectDoctor = new ParamSelectDoctor();
        paramSelectDoctor.setDoctorName(doctorName);
        paramSelectDoctor.sethId(hospitalId);
        paramSelectDoctor.setLoginState(doctorStart);
        if(startTime!=null){
            paramSelectDoctor.setStart(SimpleFormatutiles.stringdate(SimpleFormatutiles.DATE_TIME_PATTERN,startTime));
        }paramSelectDoctor.setStart(null);
        if(endTime!=null){
            paramSelectDoctor.setEnd(SimpleFormatutiles.stringdate(SimpleFormatutiles.DATE_TIME_PATTERN,endTime));
        }paramSelectDoctor.setEnd(null);
        if(doctorSex==null || doctorSex==3){
            paramSelectDoctor.setSex(null);
        }else{
            paramSelectDoctor.setSex(doctorSex);
        }
        paramSelectDoctor.setPage((currentPage-1)*limit);
        paramSelectDoctor.setLimit(limit);

       return doctorMapper.selectDoctor(paramSelectDoctor);
    }

    /**
     * 统计医生数据条数
     * @param doctorName
     * @param hospitalId
     * @param doctorStart
     * @param startTime
     * @param endTime
     * @param doctorSex
     * @return
     */
    @Override
    public Integer countDoctor(String doctorName, Integer hospitalId, Integer doctorStart, String startTime, String endTime, Integer doctorSex) {
        ParamSelectDoctor  paramSelectDoctor = new ParamSelectDoctor();
        paramSelectDoctor.setDoctorName(doctorName);
        paramSelectDoctor.sethId(hospitalId);
        paramSelectDoctor.setLoginState(doctorStart);
        if(startTime!=null){
            paramSelectDoctor.setStart(SimpleFormatutiles.stringdate(SimpleFormatutiles.DATE_TIME_PATTERN,startTime));
        }paramSelectDoctor.setStart(null);
        if(endTime!=null){
            paramSelectDoctor.setEnd(SimpleFormatutiles.stringdate(SimpleFormatutiles.DATE_TIME_PATTERN,endTime));
        }paramSelectDoctor.setEnd(null);
        if(doctorSex== null || doctorSex==3){
            paramSelectDoctor.setSex(null);
        }else{
            paramSelectDoctor.setSex(doctorSex);
        }
        return doctorMapper.countDoctor(paramSelectDoctor);
    }

    /**
     * 根据医生ID查询医生详细信息
     * @param doctorId
     * @return
     */
    @Override
    public ResultDoctorInfo selectDoctorInformation(Integer doctorId) {
        ResultDoctorInfo doctorInfo = doctorMapper.selectDoctorInformation(doctorId); //查询医生基本信息
        return doctorInfo;
    }

    /**
     * 根据医生ID和具体某一天 查询医生诊疗活动
     * @param doctorId
     * @param
     * @return
     */
    @Override
    public List<ResultDoctorConsulting> selectDoctorConsulting(Integer doctorId, String dateStr) {
        Date date =null;
        if(dateStr!=null){
            try {
                date= new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        String year=String.format("%tY", date);
        String mon=String .format("%tm", date);
        String day=String .format("%td", date);
        return doctorMapper.selectDoctorConsulting(doctorId,year,mon,day);
    }

    /**
     * 医生账号停用/启用
     * @param doctorId
     * @return
     */
    @Override
    public String stopStartUp(Integer doctorId,Integer state) {
       /* if(state==1){
            //把状态改为2停用
            state=2;
        }else{
            state=1;
        }*/
        //查询医生账号状态
        return doctorMapper.stopStartUp(doctorId,state)>0?"成功":"失败";
    }

    @Override
    public List<HospitalDoctorResultBean> selectHospitalAndDoctor() {
        return doctorMapper.selectHospitalAndDoctor();
    }

    @Autowired
    private PositionMapper positionMapper;
    @Override
    public List<Position> selectPosition() {
        return positionMapper.selectPosition();
    }
}
